const { json } = require('body-parser');
const express = require('express');
const mysql = require('mysql');
const app = express();
app.use(express.static('views'))
const {geFileList} = require('./untils/readFiles.js') 

app.get('/', (req, res) => {
    res.sendFile(__dirname + '/index.html')
})

const connection = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: 'root',
    database: 'features'
})
connection.getConnection(err => {
    if (err) throw err;
    console.log('数据库连接成功!');
})


//数据库重连
// connection.on('error',function(err){
//     if(err.code === 'PROTOCOL_CONNECTION_LOST'){
//         connection.connect(err => {
//             if(err) throw err;

//             console.log("数据库重连成功!");
//         })
//     }else{
//         throw err;
//     }
// })
app.get("/downloadFile",(req,res) => {
    console.log(req.query)
    let file = req.query.file;
    res.download(file,err=>{
        if(err) throw err;
    })
})

app.get("/readFiles", (req, res) => {
    var filesList = geFileList("e:/数据/数据");
    res.send(filesList)
})


app.get("/getData", (req, res) => {
    console.log("start getdata")
    console.log(req.query)
    let sql = "";
    console.log(req.query.name)
    if (req.query.name == '' || req.query.name == undefined) {
        sql = "select * from t_item";
    } else {
        sql = "select * from t_item where geneName = '" + req.query.name + "'";
    }
    connection.query(sql, function (err, results) {
        // connection.release()
        if (err) {
            console.log(err)
            res.send({ success: false, msg: err, data: null });
        } else {
            // console.log(results)
            res.send(results);
        }
    })
})


app.listen(8080, () => {
    console.log('running at server http://localhost:8080');
})
